package com.arges.web.controller;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.arges.web.common.AESUtils;
import com.arges.web.common.Constant;
import com.arges.web.common.GsonUtils;
import com.arges.web.domain.bean.DocBean;
import com.arges.web.domain.pojo.Doc;
import com.arges.web.page.PageInfo;
import com.arges.web.page.WebUtils;
import com.arges.web.service.DocManager;

/**
 * doctor控制层
 * 
 * @author zxj
 *
 */
@Controller
@Scope("prototype")
@RequestMapping("/doc")
public class DocController
{

	@Autowired
	private DocManager docManager;

	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/getPageDoc.do")
	public String getPageDoc(HttpServletRequest request, ModelMap map)
	{

		PageInfo<DocBean> pageInfo = null;
		String userName = request.getParameter("userName");
		if (!StringUtils.isNotBlank(userName))
		{
			userName = "";
		}
		userName = userName.replaceAll("\\_", "\\\\_");

		String pageString = request.getParameter("PageInfo");
		if (StringUtils.isNotBlank(pageString))
		{
			pageInfo = GsonUtils.stringToObject(pageString, PageInfo.class);
		}
		else
		{
			pageInfo = new PageInfo<DocBean>();
		}

		try
		{
			PageInfo<Doc> pojopInfo = WebUtils.beanToPojoPage(pageInfo, Doc.class);
			pageInfo = docManager.getPageDoc(pojopInfo, userName);
		}
		catch (Exception e)
		{
			System.out.println("getPageDoc is error. e=" + e);
		}
		userName = userName.replaceAll("\\\\_", "\\_");
		map.put("chaxun", userName);
		map.put("docBeanList", pageInfo.getResult());
		pageInfo.setResult(null);
		map.put("docPageInfo", GsonUtils.objectToJson(pageInfo));
		return "doctor/doc";
	}

	/**
	 * 添加用户时验证用户名是否已经存在
	 * 
	 * @param userName
	 * @return
	 */
	@RequestMapping(value = "/findDocByName.do")
	@ResponseBody
	public String findDocByName(HttpServletRequest request)
	{
		String name = request.getParameter("name");
		DocBean docBean = null;
		try
		{
			if (StringUtils.isNotBlank(name))
			{
				docBean = docManager.getByName(name);
			}
			if (null != docBean)
			{
				return Constant.SUCCESS;
			}
		}
		catch (Exception e)
		{
			System.out.println(e);
			return "Exception";
		}

		return Constant.ERROR;
	}

	@RequestMapping(value = "/saveOrUpdate.do")
	@ResponseBody
	public String saveOrUpdate(HttpServletRequest request, DocBean docBean)
	{
		String result = Constant.SUCCESS;
		String pmsn = docBean.getPmsn();
		if (pmsn.equals("管理员权限"))
		{
			docBean.setPmsn("1");
		}
		else if (pmsn.equals("操作员权限"))
		{
			docBean.setPmsn("2");
		}
		else
		{
			docBean.setPmsn("3");
		}
		try
		{
			DocBean oldDocBean = docManager.getByName(docBean.getName());
			if (null == oldDocBean)
			{
				Doc doc = new Doc();
				BeanUtils.copyProperties(docBean, doc);
				doc.setStatus(1);
				doc.setPassword(AESUtils.toSHA256(doc.getPassword()));
				docManager.save(doc);
			}
			else
			{
				if (docBean.getPassword() != null && docBean.getPassword().equals(""))
				{
					oldDocBean.setPassword(AESUtils.toSHA256(docBean.getPassword()));
					oldDocBean.setPmsn(docBean.getPmsn());
					oldDocBean.setEno(docBean.getEno());
					Doc doc = new Doc();
					BeanUtils.copyProperties(oldDocBean, doc);
					docManager.update(doc);
				}
			}
		}
		catch (Exception e)
		{
			System.out.println("saveOrUpdate is error,error is" + e);
			result = Constant.ERROR;
		}

		return result;
	}

	/**
	 * 根据ID获取用户信息
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/findDocById.do")
	@ResponseBody
	public DocBean findDocById(HttpServletRequest request)
	{
		String id = request.getParameter("id");
		DocBean docBean = null;
		try
		{
			docBean = docManager.findDocById(Long.parseLong(id));
		}
		catch (Exception e)
		{
			System.out.println("findDocById is error,error is" + e);
			docBean = null;
		}
		return docBean;
	}

	/**
	 * 删除、批量删除用户
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/deleteByIds.do")
	@ResponseBody
	public String deleteByIds(HttpServletRequest request)
	{
		String result = Constant.SUCCESS;
		String ids = request.getParameter("ids");
		try
		{
			docManager.deleteByIds(ids);
		}
		catch (Exception e)
		{
			System.out.println("findDocById is error,error is" + e);
			result = Constant.ERROR;
		}
		return result;
	}

	/**
	 * 冻结、批量冻结用户
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/unFreeByIds.do")
	@ResponseBody
	public String unFreeByIds(HttpServletRequest request)
	{
		String result = Constant.SUCCESS;
		String ids = request.getParameter("ids");
		try
		{
			docManager.unFreeByIds(ids);
		}
		catch (Exception e)
		{
			System.out.println("findDocById is error,error is" + e);
			result = Constant.ERROR;
		}
		return result;
	}

	/**
	 * 解冻、批量解冻用户
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/freeByIds.do")
	@ResponseBody
	public String freeByIds(HttpServletRequest request)
	{
		String result = Constant.SUCCESS;
		String ids = request.getParameter("ids");
		try
		{
			docManager.freeByIds(ids);
		}
		catch (Exception e)
		{
			System.out.println("findDocById is error,error is" + e);
			result = Constant.ERROR;
		}
		return result;
	}

	/**
	 * 添加用户时验证eno
	 * 
	 * @param eno
	 * @return
	 */
	@RequestMapping(value = "/findWorkByEno.do")
	@ResponseBody
	public String findWorkByEno(HttpServletRequest request)
	{
		String eno = request.getParameter("eno");
		DocBean docBean = null;
		try
		{
			if (StringUtils.isNotBlank(eno))
			{
				docBean = docManager.findWorkByEno(eno);
			}
			if (null != docBean)
			{
				return Constant.SUCCESS;
			}
		}
		catch (Exception e)
		{
			System.out.println(e);
			return "Exception";
		}

		return Constant.ERROR;
	}
}
